const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://taosha_user:apyl8XWP6LSW4JHs@101.35.6.34:27722/taosha";

async function main() {
  const client = await MongoClient.connect(url, {useNewUrlParser: true, useUnifiedTopology: true});
  const db = client.db('taosha');


  let topics = await db.collection('topics')
    .find(
      {tab: 'original'}
    ).skip(0).limit(10000)
    .toArray();


  for (let i = 0; i < topics.length; i++) {
    let topic = topics[i];
    let content = topic.content;
    let intro = content.replace(/[^_/:：,.;'，。、；a-zA-Z0-9\u4e00-\u9fa5]/g, ' ').replace(/\s+/g, ' ').trim().slice(0, 300);
    console.log(intro);
    console.log();


    try {
      let result = await db.collection('topics').updateOne({_id: topic._id}, {
        $set: {
          intro: intro
        }
      });
    } catch (e) {
      console.error(e.message);
    }
  }

  client.close();

}

main();
